package org.example.server.service.impl;


import org.example.dto.DepartmentDTO;
import org.example.dto.ScheNumDTO;
import org.example.entity.Department;
import org.example.entity.ScheNumInfo;
import org.example.server.mapper.DepartmentMapper;
import org.example.server.mapper.ScheNumInfoMapper;
import org.example.server.service.ScheNumInfoService;
import org.example.vo.ScheNumVO;
import org.example.vo.ScheNumsVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 医生号源表(ScheNumInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-12-31 15:54:58
 */
@Service
public class ScheNumInfoServiceImpl implements ScheNumInfoService {
    private static final Logger log = LoggerFactory.getLogger(ScheNumInfoServiceImpl.class);
    @Resource
    private ScheNumInfoMapper scheNumInfoMapper;
    @Resource
    private DepartmentMapper departmentMapper;


    @Override
    public ScheNumVO getScheNumList(ScheNumDTO scheNumDTO) {

        ScheNumVO scheNumVo = new ScheNumVO();

        scheNumVo.setDeptCode(scheNumDTO.getDeptCode());
        Department department = departmentMapper.selectByCode(scheNumDTO);

        scheNumVo.setDeptName(department.getDepartmentName());

        List<ScheNumsVO> scheNumsVOListList = new ArrayList<>();
        ScheNumsVO scheNumsVO = new ScheNumsVO();


        List<ScheNumInfo> scheNumInfoList = scheNumInfoMapper.select(scheNumDTO);

        if (scheNumInfoList != null) {
            scheNumInfoList.forEach(scheNumInfo -> {
                BeanUtils.copyProperties(scheNumInfo, scheNumsVO);


                // 创建SimpleDateFormat对象，指定格式为yy-MM-dd
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

                // 将Date对象格式化为字符串
                String formattedDateStr = formatter.format(scheNumInfo.getScheDate());


                scheNumsVO.setScheDate(formattedDateStr);


                scheNumsVO.setDeptName(department.getDepartmentName());
                LocalTime eightOClock = LocalTime.of(8, 30);
                LocalTime twelveOClock = LocalTime.of(12, 0);


                LocalTime fourteenOClock = LocalTime.of(14, 0);
                LocalTime seventeenOClock = LocalTime.of(17, 0);
                if (scheNumInfo.getStartTime().isAfter(eightOClock) && scheNumInfo.getEndTime().isBefore(twelveOClock)) {
                    scheNumsVO.setApm("上午");
                } else if (scheNumInfo.getStartTime().isAfter(fourteenOClock) && scheNumInfo.getEndTime().isBefore(seventeenOClock)) {
                    scheNumsVO.setApm("下午");
                }

                LocalDate localDate = scheNumInfo.getScheDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                DayOfWeek dayOfWeek = localDate.getDayOfWeek();
                switch (dayOfWeek) {
                    case MONDAY:
                        scheNumsVO.setWeek("周一");
                        break;
                    case TUESDAY:
                        scheNumsVO.setWeek("周二");
                        break;
                    case WEDNESDAY:
                        scheNumsVO.setWeek("周三");
                    case THURSDAY:
                        scheNumsVO.setWeek("周四");
                        break;
                    case FRIDAY:
                        scheNumsVO.setWeek("周五");
                        break;
                    case SATURDAY:
                        scheNumsVO.setWeek("周六");
                        log.info("今天是周末,隔天再来吧");
                        break;
                    case SUNDAY:
                        scheNumsVO.setWeek("周日");
                        log.info("今天是周末,隔天再来吧");
                        break;
                    default:
                        scheNumsVO.setWeek("未知");
                }

                LocalDate now = LocalDate.now();

                long between = ChronoUnit.DAYS.between(now, localDate);
                if (between > 0) {
                    scheNumsVO.setScheDateTips(between + "天后");
                } else if (between == 0) {
                    scheNumsVO.setScheDateTips("今天");
                }else {
                    scheNumsVO.setScheDateTips(between+"天前");
                }
                scheNumsVO.setScheduleId(scheNumDTO.getScheduleId());

                scheNumsVOListList.add(scheNumsVO);
            });
        }
        scheNumVo.setScheNums(scheNumsVOListList);

        return scheNumVo;
    }
}
